<!--
 * @Descripttion: 
 * @version: 版本
 * @Author: Simon
 * @Date: 2021-04-15 17:25:53
 * @LastEditors: Simon
 * @LastEditTime: 2021-04-16 16:28:37
-->
<template>
  <div class="user">
    <div class="page-head">
      <div class="page-bread">
        <div class="bread-prev" @click="back">
          频道管理
        </div>
        <div class="bread-divider">
          /
        </div>
        <div class="bread-now">
          栏目管理
        </div>
      </div>
      <div class="page-tool">
        <!-- <search-input placeholder="栏目名称" @search="searchChange" /> -->
        <div
          class="tool-btn"
          @click="open('add')"
        >
          新增栏目
        </div>
      </div>
    </div>
    <div class="page-body">
      <div class="table_data">
        <el-scrollbar class="scroll-bar">
          <el-table :data="tableData" stripe row-key="column_id" class="my-table">
            <el-table-column
              width="20"
            />
            <el-table-column
              prop="name"
              label="栏目名称"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column
              prop="remark"
              label="备注"
              :show-overflow-tooltip="true"
            >
            </el-table-column>

            <el-table-column label="操作" width="120" align="center">
              <template slot-scope="{ row }">
                <div class="operation-col">
                  <div
                    class="txt-btn ope-btn primary"
                    @click="open('edit', row)"
                  >
                    修改
                  </div>

                  <div
                    class="txt-btn error"
                    @click="doDelete(row)"
                  >
                    删除
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-scrollbar>
      </div>
      <div class="page-footer">
        <pagina-tion
          class="page-pager"
          v-show="total >= 0"
          :background="true"
          :total="total"
          :offset.sync="offset"
          :limit.sync="limit"
          @pagination="getChangePage"
        />
        <button class="page-button">确定</button>
      </div>
    </div>
    <!-- 新增和修改弹框 -->
    <el-dialog
      :visible.sync="model.visible"
      width="450px"
      :title="model.type === 'add' ? '新增栏目' : '修改栏目'"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="dialog-body">
        <el-form
          ref="form"
          :model="form"
          label-width="110px"
          label-position="left"
        >
          <el-form-item label="栏目名称" class="required">
            <el-input
              name="columnname"
              v-model="form.name"
              v-validate="'required'"
              maxlength="6"
              show-word-limit
            />
            <div v-show="errors.has('columnname')" class="el-form-item__error">
              {{ errors.first("columnname") }}
            </div>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              v-model.trim="form.remark"
              maxlength="32"
              show-word-limit />
            <!-- <div v-show="errors.has('remark')" class="el-form-item__error">
              {{ errors.first("remark") }}
            </div> -->
          </el-form-item>
          
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">保 存</el-button>
      </span>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog
      :visible.sync="visibility"
      width="430px"
      height="100px"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="dialog-content comfirm-body">
        <img class="comfirm-ico" src="~assets/img/warning.png" />
        <div class="comfirm-txt">
          <div>确定删除该条数据？</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibility = false">取 消</el-button>
        <el-button type="primary" @click="columnDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { format } from "silly-datetime";
import * as api from "@/api";
import { limit, middleLimit } from "@/consts/consts";
import { columnFormKey } from "@/consts/formKey";
export default {
  data() {
    return {
      btnPermiss: [],
      limit: document.body.clientHeight>= 770 ? limit : middleLimit,
      total: 0,
      offset: 0,
      tableData: [],
      format,
      search: "",
      roleSel: [],
      filterRole: [],
      model: {
        type: "add",
        visible: false,
      },
      column_id: "",
      form: { ...columnFormKey,channel_id:this.$route.params.id },
      visibility: false,
    };
  },

  methods: {
    back() {
      this.$router.go(-1)
    },
    open(type, row = null) {
      this.$validator.reset();
      this.model.type = type;
      if (row) {
        for (const k in columnFormKey) {
          this.form[k] = row[k];
        }
        this.column_id= row.column_id;
      }
      this.model.visible = true;
    },

    doDelete(row) {
      this.column_id = row.column_id;
      this.visibility = true;
    },

    getChangePage(val) {
      this.offset = val.offset;
      this.columnList();
    },

    // searchChange(e) {
    //   this.offset = 0
    //   this.search = e;
    //   this.columnList();
    // },


    close() {
      this.model.visible = false;
      this.form = { ...columnFormKey,channel_id:this.$route.params.id };
    },

    submit() {
      this.$validator.validateAll().then((valid) => {
        if (valid) {
          // if (this.form.nick) {
          //   let reg = /^[\u4E00-\u9FA5]{1,8}$/.test(this.form.nick);
          //   if (!reg) {
          //     this.$message.warning("昵称请输入8位以内中文");
          //     return false;
          //   }
          // }
          this.addOrEdit();
        } else {
          this.$message.warning("有信息未填写正确");
        }
      });
    },

    async columnList() {
      const { code, data, total } = await api.column(this).columnList({
        channel_id:this.$route.params.id,
        offset: this.offset,
        limit: this.limit,
      });
      if (code === 0) {
        this.tableData = data;
        this.total = total;
      }
    },



    async addOrEdit() {
      let fn = this.model.type === "add" ? "columnAdd" : "columnUpdate";
      const { code } = await api.column(this)[fn](this.form, this.column_id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.close();
        this.columnList();
      }
    },

    async columnDelete() {
      const { code } = await api.column(this).columnDelete(this.column_id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.column_id = "";
        this.visibility = false;
        if (this.total % this.limit === 1) {
          if (this.offset >= this.limit) {
            this.offset -= this.limit;
          }
        }
        this.columnList();
      }
    },
  },
  created() {
    this.columnList();
    // let permission = JSON.parse(localStorage.getItem("permission"));
    // let curMenu = permission.find(item => item.txt === "频道管理")
    // this.btnPermiss = curMenu.btns
  },

};
</script>

<style lang="stylus">
.User {
  
}
</style>